package com.mylearn.myblog.controller;

import com.mylearn.myblog.entity.Blog;
import com.mylearn.myblog.service.BlogService;
import com.mylearn.myblog.util.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/blog")
@RestController
@Api(value = "博客接口",description = "博客接口")
public class BlogController {

	@Autowired
	private BlogService blogService;

	@ApiOperation(value = "根据条件分页查询博客",notes = "根据条件分页查询博客")
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页显示数", dataType = "Integer", paramType = "query", required = true)
	})
	public BaseResult list(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody Blog blog){
		if(page<1){
			return BaseResult.notOk();
		}
		Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id");
		Page<Blog> blogs = blogService.listBlog(pageable, blog);
		return BaseResult.ok(blogs);
	}

	@ApiOperation(value = "新增分类",notes = "新增分类")
	@RequestMapping(value = "/addblog",method = RequestMethod.POST)
	public BaseResult addblog(@RequestBody Blog blog){
		Blog blog1 = blogService.getBlogByTitle(blog.getTitle());
		if (blog1 != null){
			return BaseResult.notOk();
		}
		Blog b = blogService.saveBlog(blog);
		if (b == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(b);

	}

	@ApiOperation(value = "根据id查询博客",notes = "根据id查询博客")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "博客id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/editInput/{id}",method = RequestMethod.GET)
	public BaseResult editInput(@PathVariable Long id){
		return BaseResult.ok(blogService.getBlog(id));
	}

	@ApiOperation(value = "修改/更新博客",notes = "修改/更新博客")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "博客id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/editBlog/{id}",method = RequestMethod.POST)
	public BaseResult editBlog(@RequestBody Blog blog,@PathVariable Long id){
		Blog blog1 = blogService.getBlogByTitle(blog.getTitle());
		if(blog1 != null){
			return BaseResult.notOk();
		}
		Blog b = blogService.updateBlog(id,blog);
		if (b == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(b);
	}
	@ApiOperation(value = "根据id删除博客",notes = "根据id删除博客")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "博客id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public BaseResult delete(@PathVariable Long id){
		Blog blog = blogService.getBlog(id);
		if (blog !=null){
			blogService.deleteBlog(id);
			return BaseResult.ok();
		}
		return BaseResult.notOk();
	}
}
